﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ImapX.Parsing.Tokens
{
    public class NString : Token
    {
        public string Value { get; internal set; }

        public NString() : this(true) { }

        public NString(bool canBeNIL = true)
            : base(canBeNIL)
        {
        }

        protected override string ParseBegin(string input)
        {
            var match = Match(input, @"""(?<unquoted>((?<!\\)\\""|[^""])*)""");

            if (!match.Success) throw new ImapException("Failed to parse quoted string from: " + input);

            Value = match.Groups["unquoted"].Value;

            string leftover = input.Substring(match.Length);

            return leftover;
        }

        public override string ToString()
        {
            return Value;
        }
    }
}
